﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace SimpleSamplesXNA
{
    public struct animation
    {
        //A Timer variable
        public float timer;
        //The interval (100 milliseconds)
        public float interval;
        //Current frame holder (start at 1)
        public int currentFrame;
        //Width of a single sprite image, not the whole Sprite Sheet
        public int spriteWidth;
        //Height of a single sprite image, not the whole Sprite Sheet
        public int spriteHeight;
        //where on sprite sheet to draw
        public Rectangle sourceRect;
        //holds texture data
        public Texture2D sprite;
        //number of frames
        public int numFrames;
        //scalar
        public float scalar;
        //vector origin
        public Vector2 origin;
    }

    //Works with one sprite sheet in the x dimension only.
    class SpriteAnimator
    {
        animation anim = new animation() { //defaults
            
        };


        public SpriteAnimator(Texture2D texture, float timer, float interval, int currentFrame, int spriteWidth, int spriteHeight, int numFrames, float scalar)
        {
            anim.sprite = texture;
            anim.currentFrame = currentFrame;
            anim.interval = interval;
            anim.spriteHeight = spriteHeight;
            anim.spriteWidth = spriteWidth;
            anim.timer = timer;
            anim.sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
            anim.numFrames = numFrames;
            anim.scalar = scalar;
        }




        //update current animation data
        void Update(GameTime gameTime)
        {
            
            //Increase the timer by the number of milliseconds since update was last called
            anim.timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
             
            //Check the timer is more than the chosen interval
            if (anim.timer > anim.interval)
            {
                //Show the next frame
                anim.currentFrame++;
                //Reset the timer
                anim.timer = 0f;
            }
            //If we are on the last frame, reset back to the one before the first frame (because currentFrame++ is called next so the next frame will be 1!)
            if (anim.currentFrame == anim.numFrames)
            {
                 anim.currentFrame = 0;
            }

            anim.sourceRect = new Rectangle(anim.currentFrame * anim.spriteWidth, 0, anim.spriteWidth, anim.spriteHeight);
            anim.origin = new Vector2(anim.sourceRect.Width / 2, anim.sourceRect.Height / 2);
        }




        //draws animation to screen
        void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 pos)
        {
            Update(gameTime);
            spriteBatch.Draw(anim.sprite, pos, anim.sourceRect, Color.White, 0f, anim.origin, 1.0f*anim.scalar, SpriteEffects.None, 0);
            //Code here
        }


    }
}
